/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.atomcorp.controlador;

import com.atomcorp.modelo.Cliente;
import com.atomcorp.modelo.Empresa;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import vista.SistemaFacturacion;

/**
 *
 * @author manu
 */
public class FacturaControlador implements Initializable{

    private static FacturaControlador instance;
    private Stage stage;
    private Empresa empresa;
    
    @FXML private Label lbNombre;
    @FXML private Label lbRepresentante;
    @FXML private Label lbDireccion;
    @FXML private Label lbRuc;
    
    @FXML private TextField txtRuc;
    @FXML private TextField txtCliente;
    @FXML private TextField txtDireccion;
    @FXML private TextField txtTelefono; 
    
    
     @Override
    public void initialize(URL url, ResourceBundle rb) {
        empresa = SistemaFacturacion.getInstance().getEmpresa();
        lbNombre.setText(empresa.getNombre());
        lbRepresentante.setText(empresa.getRepresentante());
        lbDireccion.setText(empresa.getDireccion());
        lbRuc.setText(empresa.getRuc());
                
    }

    public FacturaControlador() {
        instance = this;
    }

    public static FacturaControlador getInstance() {
        return instance;
    }
    
    
    @FXML
    protected void btnSeleccionarProducto(ActionEvent event) {
        stage = new Stage();
        stage.setResizable(false);
        iniciarLogin();
        stage.show();
    }
    
    public void cerrarVentana(Cliente cliente){
        System.out.println("cliente ruc :" + cliente.getIdentificacion());
        txtRuc.setText(cliente.getIdentificacion());
        txtCliente.setText(cliente.getNombres().concat(" ").concat(cliente.getApellidos()));
        txtDireccion.setText(cliente.getDireccion());
        txtTelefono.setText(cliente.getTelefono());
        stage.hide();
    }

    private void iniciarLogin() {
        try {
            replacePlantalla("buscarCliente.fxml");
        } catch (Exception ex) {
            Logger.getLogger(SistemaFacturacion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private Parent replacePlantalla(String fxml) throws Exception {
        Parent page = (Parent) FXMLLoader.load(SistemaFacturacion.class.getResource(fxml), null, new JavaFXBuilderFactory());
        Scene scene = stage.getScene();
        stage.setResizable(false);
        stage.setTitle("Sistema de Facturación");
        if (scene == null) {
            scene = new Scene(page);
            stage.setScene(scene);
        } else {
            stage.getScene().setRoot(page);
        }
        stage.sizeToScene();
        return page;
    }

}
